//JS
layui.use(['element', 'layer', 'util', 'laydate', 'table', 'form'], function () {
    var element = layui.element,
        util = layui.util,
        form = layui.form,
        table = layui.table,
        laydate = layui.laydate,
        $ = layui.$;


    <!-- 动态表格 -->
    table.render({
        elem: '#business-table',
        toolbar: '#toolbarDemo',
        page: true,
        url: "/queryAllStageTime",
        height: 500,
        cols: [
            [ //标题栏
                {field: 'stage', title: '业务类型', templet: '#stageTpl', align: 'center'},
                {field: 'startTime', title: '开始时间', align: 'center'},
                {field: 'endTime', title: '结束时间', align: 'center'},
                {title: '操作', width: 120, toolbar: '#businessBtn', align: 'center'}
            ]
        ]
    });
//监听行工具事件
    table.on('tool(business-table)', function (obj) {
        let data = obj.data;
        if (obj.event === 'edit') {
            console.log(data);
            layer.open({
                title: '时间设置'
                , offset: 100
                , type: 1
                , skin: 'layui-layer-rim'
                , shadeClose: true
                , area: ['800px', '340px']
                , success: function () {
                    $("#stage1").val(data.stage);
                    let state;
                    if (data.stage === 1 || data.stage === '1') {
                        state = '任务书阶段'
                    } else if (data.stage === 2 || data.stage === '2') {
                        state = '开题报告阶段'
                    } else if (data.stage === 3 || data.stage === '3') {
                        state = '中期检查阶段'
                    } else if (data.stage === 4 || data.stage === '4') {
                        state = '论文初稿阶段'
                    } else if (data.stage === 5 || data.stage === '5') {
                        state = '论文定稿阶段'
                    } else if (data.stage === 6 || data.stage === '6') {
                        state = '论文终稿阶段'
                    }
                    $("#stage").html(state);
                }
                , content: $('#business-edit-box')
                , end: function () {
                    $('#business-edit-box').css('display', 'none');
                }
            });
        }
    });

    form.on('submit(edit)', function (data) {
        layer.confirm('确认提交？', function () {
            // console.log(data.field); //当前容器的全部表单字段，名值对形式：{name: value}
            // let taskData = JSON.stringify(data.field);
            // console.log(taskData)
            // console.log(typeof taskData)

            let stageId = data.field.stage
            let startTime = data.field.startTime
            let endTime = data.field.endTime

            // console.log(stageId)
            // console.log(typeof stageId)
            // console.log(startTime)
            // console.log(typeof startTime)
            // console.log(endTime)
            // console.log(compare(startTime,endTime))
            let loadPage = layui.layer.load(0, {shade: [0.5, '#fff']});
            $.ajax({
                url: "/updateStageTime"
                , type: 'post'
                , data: {
                    // taskData: taskData
                    stageId: stageId,
                    startTime: startTime,
                    endTime: endTime
                }
                , dataType: 'json'
                , success: function (data) {  //调用接口成功
                    if (data === true || data === 'true') {
                        if (compare(startTime,endTime) === true || compare(startTime,endTime) === 'true'){
                            layer.close(loadPage);
                            layer.closeAll('page');
                            layui.layer.msg('修改成功！', {offset: '300px', time: 2500, anim: 0});
                            table.reload('business-table', {});
                        } else {
                            layui.layer.close(loadPage);
                            layui.layer.alert('修改失败！');
                        }

                    } else {
                        layui.layer.close(loadPage);
                        layui.layer.alert('修改失败！');
                    }
                },
                error: function () {  //接口错误
                    layui.layer.close(loadPage);
                    layui.layer.alert('服务器出错啦,请稍后再试！');
                }
            });
        });
        return false;//阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });

    //初始化时间选择盒子
    // for (let i = 1; i <= 6; i++) {
    laydate.render({
        elem: '#startTime'
    });
    laydate.render({
        elem: '#endTime'
    });
    // }
    //时间判断函数
    function compare(a, b) {
        var arr = a.split("-");
        var starttime = new Date(arr[0], arr[1], arr[2]);
        var starttimes = starttime.getTime();

        var arrs = b.split("-");
        var endtime = new Date(arrs[0], arrs[1], arrs[2]);
        var endtimes = endtime.getTime();

        if (endtimes > starttimes) {
            return true;
        }
        else{
            return false;
        }
    }
});
